/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.data.crud;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author thiago
 */
public class CrudEstados {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("wabtPU");

    public void persist(Object object) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public int getNovoCodigo() {
        EntityManager em = emf.createEntityManager();
        try {
            int max = Integer.parseInt(em.createQuery("SELECT MAX(e.codigoest) FROM Estados e").getResultList().get(0).toString());
            return (max + 1);
        } catch (Exception e) {
            return 0;
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Estados> getAll() {

        EntityManager em = emf.createEntityManager();
        try {
            return em.createNamedQuery("Estados.findAll").getResultList();
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public br.data.model.Estados getEstado(int codigo) {

        EntityManager em = emf.createEntityManager();
        try {
            return em.find(br.data.model.Estados.class, codigo);
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public void remove(int codigo) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            br.data.model.Estados est;
            est = em.find(br.data.model.Estados.class, codigo);
            em.remove(est);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public void update(int codigo, String nome) {

        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();

            br.data.model.Estados est;
            est = em.find(br.data.model.Estados.class, codigo);
            est.setCodigoest(codigo);
            est.setNome(nome);

            em.merge(est);

            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
}
